package com.qf.mvc2202.controller;

import com.qf.mvc2202.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * 演示各种收参方式
 *
 * 这几种收参方式，要求参数的格式如下 ，get请求问号后面带参或者post请求体中带参都可以
 * id=100&name=zhangsan
 *
 */
@Controller
@RequestMapping("/param")
public class ParamController {


    /**
     * 演示基本类型的参数收参
     * http://localhost:8080/param/test1?id=100&name=luffy&gender=false&birth=2000/01/01 12:12:12
     *
     *
     * http://localhost:8080/param/test1?id=100&name=luffy&gender=false&birth=2000-01-01 12:12:12
     * @return
     */
    @RequestMapping("/test1")
    public String test1(Integer id,
                        String name,
                        Boolean gender,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date birth) {
        System.out.println(id);
        System.out.println(name);
        System.out.println(gender);
        System.out.println(birth);

        return "/hello.jsp";
    }


    /**
     * 演示实体收参
     * 注意，请求中 的参数名需要与实体类型中的属性名一致
     * @return
     *
     *  http://localhost:8080/param/test2?id=1&name=zzz&gender=false&birth=2018-12-12 12:20:30
     */
    @RequestMapping("/test2")
    public String test2(User user){
        System.out.println(user);
        return "/hello.jsp";
    }

    /**
     * 演示数组收参
     * 可以接收的参数的格式为“：hobby=football&hobby=basketball
     * @param hobby
     * @return
     */
    @RequestMapping("/test3")
    public String test3(String[] hobby){
        for (String s : hobby) {
            System.out.println(s);
        }
        return "/hello.jsp";
    }

    @RequestMapping("/test4")
    public String test4(User user){
        for (String s : user.getHobby()) {
            System.out.println(s);
        }
        return "/hello.jsp";
    }

    /**
     * 测试路径收参
     * 注意：路径层级必须要匹配，大括号里面的变量名要与@PathVariable注解中的字符串保持一致
     * @param id
     * @return
     */
    @RequestMapping("/get/{id}")
    public String test5(@PathVariable("id") Integer id){
        System.out.println(id);
        return "/hello.jsp";
    }


    /**
     * 多级路径收参
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("/get/{id}/{name}")
    public String test6(@PathVariable("id") Integer id,@PathVariable("name") String name){
        System.out.println(id);
        System.out.println(name);
        return "/hello.jsp";
    }


}
